package com.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bean.Bargain;
import com.bean.Housepayment;
import com.bean.Indent;
import com.bean.Openingreport;
import com.bean.Project;
import com.service.BargainService;
import com.service.HousepaymentService;
import com.service.IndentService;
import com.service.MarketingService;
import com.service.OpeningreportService;
import com.service.ProjectService;
import com.service.RoomService;
import com.util.Export2;
import com.util.Export3;


@Controller
@RequestMapping("/housepayment")
public class HousePaymentcontroller {
	
	@Autowired
	private HousepaymentService hs;
	
	
	@Autowired
	private BargainService bs;
	
	
	@Autowired
	private IndentService is;
	
	
	@Autowired
	private MarketingService ms;
	
	
	@Autowired
	private ProjectService ps;
	
	@Autowired
	private OpeningreportService os;
	
	
	
	@RequestMapping("/housepayment.do")
    public String housepayment(HttpServletRequest rs,Model model){
		Integer uid = (Integer) rs.getSession().getAttribute("uid");
		Integer rid = (Integer) rs.getSession().getAttribute("rid");
		System.out.println("uid="+uid);
		
		List<Housepayment> list=new ArrayList<Housepayment>();
		List<Housepayment> list1=new ArrayList<Housepayment>();
	    if(rid==1)
	    {
	    	 list = hs.selectAllByuid(uid);
	    	/* list1 = hs.selectAllByuidHe(uid);*/
	    }
	    if(rid==2)
	    {
	       list = hs.selectAll();
	       /*list1 = hs.selectAllHe();*/
	    }
	    
	    System.out.println("size="+list1.size());
	    
	    model.addAttribute("list", list);
	    /*model.addAttribute("list1", list1);*/
	  	return "housepayment/housepaymentlist";
    }
	

	@RequestMapping("/select.do")
    public String select(Integer bid,Integer iid,HttpServletRequest rs,Model model){
		Integer uid = (Integer) rs.getSession().getAttribute("uid");
		Integer rid = (Integer) rs.getSession().getAttribute("rid");
		
		List<Housepayment> list=new ArrayList<Housepayment>();
		List<Housepayment> list1=new ArrayList<Housepayment>();
	  if(rid==1)
	    {
	    	Map<String,Object> map=new HashMap<String,Object>();
	    	map.put("bid",bid);
	    	map.put("iid",iid);
	    	map.put("userid",uid);
	    	
	    	 list = hs.selectAllByuidbidiid(map);
	    	 
	    	/* Map<String,Object> map2=new HashMap<String,Object>();
		    	map2.put("bid",bid);
		    	map2.put("iid",iid);
		    	map2.put("userid",uid);
		    	
		    	 list1 = hs.selectAllByuidHeiidbid(map2);
	    	 */
	    	 
	    	 
	    }
	    if(rid==2)
	    {
	    	
	    	Map<String,Object> map1=new HashMap<String,Object>();
	    	map1.put("bid",bid);
	    	map1.put("iid",iid); 	 
	       list = hs.selectAllbiduid(map1);
	      /* Map<String,Object> map3=new HashMap<String,Object>();
	    	map3.put("bid",bid);
	    	map3.put("iid",iid);
	    	 list1 = hs.ByuidHeuidbid(map3);
   	 */
	       
	       
	    }
	    
	    System.out.println("size="+list.size());
	    model.addAttribute("list1", list1);
	    model.addAttribute("list", list);
	    
	  	return "housepayment/housepaymentlist";
    }
	

	@RequestMapping("daochu")
    public void daochu(Integer[] b,HttpServletRequest request,HttpServletResponse response) throws IOException{
		
	
		List<Housepayment> list=new ArrayList<Housepayment>();
		
		for(Integer l:b){
			Housepayment housepayment = hs.selectByPrimaryKeyduo(l);
			list.add(housepayment);
		}
		
		response.setHeader("Content-Disposition","attachment;filename=housepayment.xls");//指定下载的文件名  
	    response.setContentType("application/vnd.ms-excel");  
		 Export3 ex = new Export3();   
		 ex.buildBaojiaExcel(list, "房款表.xls",response.getOutputStream());
		 
    }
	
	
	
	
	
	
	
	
	@RequestMapping("/delete.do")
    public String delete(Integer id,Model model){
		System.out.println("id="+id);
		Housepayment housepayment = hs.selectByPrimaryKey(id);
		System.out.println("rrrrrr"+housepayment);
		if(housepayment.getBargainid()!=null && housepayment.getIndentid()!=null)
		{
		
			if(housepayment.getBargainid()!=null){
				
			      Bargain selectByPrimaryKey = bs.selectByPrimaryKey(housepayment.getBargainid());
					int i = ms.deleteByPrimaryKey(selectByPrimaryKey.getMaketingid());
					System.out.println("删除机会8="+i);
					int j = hs.deleteByPrimaryKey(housepayment.getBargainid());
					System.out.println("删除合同7="+j);		
				}
				
				//删除订单
				if(housepayment.getIndentid()!=null){
					int v = is.deleteByPrimaryKey(housepayment.getIndentid());
					System.out.println("删除订单6="+v);
				}
			
			
			
			
			
		}else{
		
		//删除合同
		
		if(housepayment.getBargainid()!=null){
			
	      Bargain selectByPrimaryKey = bs.selectByPrimaryKey(housepayment.getBargainid());
			int i = ms.deleteByPrimaryKey(selectByPrimaryKey.getMaketingid());
			System.out.println("删除机会5="+i);
			int j = hs.deleteByPrimaryKey(housepayment.getBargainid());
			System.out.println("删除合同4="+j);		
		}
		
		//删除订单
		if(housepayment.getIndentid()!=null){
			
			
			Indent indent = is.selectByPrimaryKey(housepayment.getIndentid());
			System.out.println("删除订单90="+indent.getMarketingid());
			int i = ms.deleteByPrimaryKey(indent.getMarketingid());
			System.out.println("删除机会3="+i);
			int v = is.deleteByPrimaryKey(housepayment.getIndentid());
			System.out.println("删除订单2="+v);
		}
		
		}
		
		
			//删除房款信息
		System.out.println("删除房款信息id="+id);
			int i = hs.deleteByPrimaryKey(id);
			System.out.println("删除房款信息1="+i);
			
		
	  	return "housepayment/housepaymentlist";
    }
	

	@RequestMapping("/update.do")
    public String update(Integer id,Model model){
		
		System.out.println("id==="+id);
		
		Housepayment housepayment = hs.selectByPrimaryKey(id);
		System.out.println("housepaymentid==="+housepayment.getPaymenttype());
		
		model.addAttribute("housepayment", housepayment);
		
		return "housepayment/shoukuan";
	}
	
	
	
	@RequestMapping("/shoukuan.do")
    public String shoukuan(Integer hid,Float uncollectedMoney,Float huankuan,Model model){
		
		System.out.println("id==="+hid);
		Housepayment housepayment = hs.selectByPrimaryKey(hid);
		
		Float c=uncollectedMoney-huankuan;
		if(c>0)
		{   
			Float a=housepayment.getAccomplishmoney()+huankuan;
			Float b=housepayment.getUncollectedmoney()-huankuan;
			housepayment.setAccomplishmoney(a);
			housepayment.setUncollectedmoney(b);
			housepayment.setState("未还完");	
			int i = hs.updateByPrimaryKey(housepayment);
			System.out.println("iii==="+i);
			
		}
		if(c==0)
		{   
			Float a=housepayment.getAccomplishmoney()+huankuan;
			Float b=housepayment.getUncollectedmoney()-huankuan;
			housepayment.setAccomplishmoney(a);
			housepayment.setUncollectedmoney(b);
			housepayment.setState("已还完");	
			int i = hs.updateByPrimaryKey(housepayment);
			System.out.println("iiaaaai==="+i);
			
		}
		
		
		
		return "redirect:/housepayment/housepayment.do";
	}
	
	
	
	
	@RequestMapping("/gengming.do")
    public String gengming(Integer id,String cname,Model model,HttpServletRequest rs){
		Housepayment housepayment = hs.selectByPrimaryKey(id);
		
		String username="";
		
		try {
			 username =new String(rs.getParameter("cname").getBytes("iso8859-1"),"UTF-8");
			
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("cname==="+username);
		model.addAttribute("housepayment", housepayment);
		model.addAttribute("cname", username);
		
	   return "housepayment/gengming";
	}
	
	
	
	@RequestMapping("/huanfang.do")
    public String huanfang(Integer id,String nn,Model model,HttpServletRequest rs){
		Housepayment housepayment = hs.selectByPrimaryKey(id);
		String room="";
		try {
			 room =new String(rs.getParameter("nn").getBytes("iso8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		
		List<Openingreport> list4 = os.selectByState("已审核");
		List<Project> list1=new ArrayList<Project>();
		for(Openingreport l:list4){
			Integer pid = l.getPid();
			Project project = ps.selectByPrimaryKey(pid);
			list1.add(project);
		}
		
		model.addAttribute("housepayment", housepayment);
		model.addAttribute("room", room);
		model.addAttribute("list1", list1);
		
	   return "housepayment/huanfang";
	}
	
}
